package com.ctshk.sapp.workflow.controller;


import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.rpc.workflow.dto.ApprovalDTO;
import com.ctshk.rpc.workflow.dto.ApprovalInfoDTO;
import com.ctshk.rpc.workflow.dto.BusinessTypeEnumDTO;
import com.ctshk.rpc.workflow.req.ApprovalReq;
import com.ctshk.rpc.workflow.req.ApprovalStartReq;
import com.ctshk.rpc.workflow.req.ApprovalStatusReq;
import com.ctshk.rpc.workflow.service.ISysApprovalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import com.ctshk.common.web.support.BaseController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 审批 前端控制器
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-25
 */
@Api(value = "审批", tags = "审批中心")
@RestController
@RequestMapping("/approval")
public class SysApprovalController extends BaseController {

    @DubboReference
    private ISysApprovalService approvalService;

    @ApiOperation(value = "审批列表", notes = "我的审批中心-列表")
    @OperationLogAnnotation(operModul = OperationLogModel.APPROVAL, operType = OperationLogType.QUERY, operTitle = "查询审批列表", operDesc = "查询我的审批中心列表")
    @PostMapping("/list")
    public Result<PageResponse<ApprovalDTO>> list(@RequestBody @Valid ApprovalReq req) {
        req.setStartTime(StringUtils.isNotBlank(req.getStartTime()) ? req.getStartTime() + " 00:00:00" : null);
        req.setEndTime(StringUtils.isNotBlank(req.getEndTime()) ? req.getEndTime() + " 23:59:59" : null);
        return Result.success(approvalService.queryPage(req, getCurrentUser(getRequest()).getId()));
    }

    @ApiOperation(value = "审批详情", notes = "我的审批中心-详情")
    @OperationLogAnnotation(operModul = OperationLogModel.APPROVAL, operType = OperationLogType.QUERY, operTitle = "查询审批详情", operDesc = "'查询审批详情,审批id'+ #id")
    @GetMapping("/info/{id}")
    public Result<ApprovalInfoDTO> info(@PathVariable long id) {
        return Result.success(approvalService.query(id, getCurrentUser(getRequest()).getId()));
    }

    @ApiOperation(value = "审批", notes = "我的审批中心-审批")
    @OperationLogAnnotation(operModul = OperationLogModel.APPROVAL, operType = OperationLogType.UPDATE, operTitle = "进行审批", operDesc = "'进行审批操作,审批id:' + #id")
    @PostMapping("/status")
    public Result status(@RequestBody @Valid ApprovalStatusReq req) {
        approvalService.approval(req.getId(), req.getStatus(), getCurrentUser(getRequest()).getId(), req.getComments());
        return Result.success();
    }

    @ApiOperation(value = "审批类型", notes = "我的审批中心-查询业务类型与审批类型,两级联动")
    @OperationLogAnnotation(operModul = OperationLogModel.APPROVAL, operType = OperationLogType.QUERY, operTitle = "查询业务类型与审批类型", operDesc = "查询业务类型与审批类型,两级联动")
    @GetMapping("/enum")
    public Result<List<BusinessTypeEnumDTO>> queryEnum() {
        return Result.success(approvalService.queryEnum());
    }

    @PostMapping("/start")
    public Result start(@RequestBody ApprovalStartReq startReq) {
        return approvalService.start(startReq.getBusinessTypeId(), startReq.getApprovalTypeId(), startReq.getBusinessId(), getCurrentUser().getId(), startReq.getAmount(), startReq.getExtendParams());
    }
}
